package com.sunjob.mroassets.server.serverImpl;

import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import com.sunjob.mroassets.mapper.TimeMapper;
import com.sunjob.mroassets.server.TimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TimeServiceImpl implements TimeService {
    @Autowired
    TimeMapper timeMapper;
    @Override
    //通过实体类型查询实体列表
    public Object getTypeTime(String type) {
        //判断实体的类型
        if ("instance".equals(type)){
            //通过查询获取实例的剩余寿命信息列表
            List<Instance> instances = timeMapper.getInstanceTime();
            return instances;
        }else if ("part".equals(type)){
            //通过查询获取部件的剩余寿命信息列表
            List<Part> parts = timeMapper.getPartTime();
            return parts;
        }
        return null;
    }

    @Override
    //通过实体类型以及寿命类型查询实体列表
    public Object getEntityTimeByLimity(String type, Integer timeType) {
        //通过寿命类型初始化剩余寿命的限制
        int startTime = -1;
        int endTime = 0;
        switch (timeType){
            case 1:
                break;
            case 2:
                startTime = 0;
                endTime = 7;
                break;
            case 3:
                startTime = 7;
                endTime = 30;
                break;
            case 4:
                startTime = 30;
                endTime = 90;
                break;
            case 5:
                startTime = 90;
                endTime = 180;
                break;
            case 6:
                startTime = 180;
                endTime = 365;
                break;
            case 7:
                startTime = 365;
                endTime = 1095;
                break;
            case 8:
                startTime = 1095;
                endTime = 36500;
                break;
        }
        //判断实体的类型
        if ("instance".equals(type)){
            //通过剩余寿命查询实例的信息列表
            List<Instance> instances = timeMapper.getInstanceTimeByLimity(startTime,endTime);
            return instances;
        }else if ("part".equals(type)){
            //通过剩余寿命查询部件的信息列表
            List<Part> parts = timeMapper.getPartTimeByLimity(startTime,endTime);
            return parts;
        }
        return null;
    }

    @Override
    //依次调用服务层进行处理
    public void updateEntityTime() {
        //修改实例列表中正常、故障状态的实例剩余寿命
        timeMapper.updateInstanceTime();
        //修改部件列表中正常状态的部件剩余寿命
        timeMapper.updatePartLastTime();
        //修改零件列表中正常状态的零件剩余寿命
        timeMapper.updateAccessoryLastTime();
    }
}
